<?php

class ArticleController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Article'),
        ));
    }

    public function actionCreate() {
        $model = new Article;
        $this->savecmsDescription($model);

        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'Article');
        $this->savecmsDescription($model);
        $this->render('update', array(
            'model' => $model,
        ));
    }
    
    
    public function actionviewarticles($id){   
        $this->layout = 'innerpage';
         $this->render('articles-page', array(
            'model' => $this->loadModel($id, 'Article'),
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'Article')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionAdmin() {
        $model = new Article('search');
        $model->unsetAttributes();

        if (isset($_GET['Article']))
            $model->setAttributes($_GET['Article']);

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    private function savecmsDescription($model) {
        $uploadeDir = YiiBase::getPathOfAlias('webroot') . aqConfig::getUploadDir() . 'article-image/';
        $rfilename = '';
        if (isset($_POST['Article'])) {
            $rfilename = $model->photo;
            $model->setattributes($_POST['Article']);
            $model->setAttribute('photo', $rfilename);
            $model->setAttribute('create_time ', date('Y-m-d'));
            if ($model->validate()) {
                $transaction = Yii::app()->getDb()->beginTransaction();
                try {
                    if (isset($_FILES['Article']['name']['photo']) && !empty($_FILES['Article']['name']['photo'])) {
                        if (!empty($model->photo)) {
                            
                            if (file_exists($uploadeDir . $model->photo)) {
                                unlink($uploadeDir . $model->photo);
                              
                            }
                        }
                        $model->photo = CUploadedFile::getInstance($model, 'photo');
                         $pathinfo= $uploadeDir.$model->photo ;
                        $fileInfo = pathinfo($pathinfo);
                        phpinfo();
                        $ext = $fileInfo['extension'];
                        $image = $_FILES['Article']['tmp_name'];
                        $mainTempImages = $image['photo'];
                        $rfilename = guid() . "." . strtolower($ext);
                        $uploadnewDir = $uploadeDir . $rfilename;
                        $model->photo->saveAs($uploadnewDir);
                        $image = Yii::app()->image->load($uploadnewDir);
                        $image->resize(232, 212, Image::NONE);
                        $image->save();                        
                        $model->setAttribute('photo',$rfilename);
                    }
                    $transaction->commit();
                } catch (Exception $e) {
                    $transaction->rollback();
                    throw $e;
                }
                if ($model->save()) {
                    $this->redirect(array('view', 'id' => $model->id));
                }
            }
        }
    }

}
